
import store from '@/store';
import router from '@/router';
import {getCookie} from '@/utils/auth';

const whitePath = ['/login']

router.beforeEach(async (to,from,next)=>{
    if(getCookie()){//本地cookie有信息
        if(to.path==='/login'){//不用重复登录
            next({path:'/home'})
        }else{//角色对应视图权限
            let rolesFlag = store.getters.getRoles && store.getters.getRoles.length>0;
            if(rolesFlag){//以获取角色
                next()
            }else{
                const {roles} = await store.dispatch('users/getRoles');//获取角色
                await store.dispatch('permission/setRoutes',roles);
                next()
            }   
        }
        next()
    }else{
       if(whitePath.includes(to.path)){
            next()
       }else{
            next({path:`/login?redirect=${to.path}`})
       }
    }
})
